﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyOA.BLL;
using MyOA.Model;
using AjaxControlToolkit;

namespace MyOA.UI
{
    public partial class LeftTree : System.Web.UI.Page
    {
        LeftPanelManage manage = null;//在初始化时登陆成功时实例化
        string loginId = string.Empty;
        /// <summary>
        /// 绑定数据
        /// </summary>
        protected void dataBind()
        {
            //manage = new LeftPanelManage("admin");
            
            if (manage!=null)
            {
                
                IList<SysFun> mainMenus = null;
                if (Cache["mainMenus" + loginId] != null)
                {
                    mainMenus = (List<SysFun>)Cache["mainMenus" + loginId];
                }
                else
                {
                    mainMenus = manage.getMainMenu();

                    //添加缓存
                    Cache.Insert("mainMenus" + loginId, mainMenus, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);//把数据放入缓存
                }
                int i = 0;
                foreach (SysFun main in mainMenus)
                {
                    i++;
                    Label mainMenu = new Label();
                    mainMenu.ID = "lbl" + i.ToString();
                    mainMenu.Text = main.DisplayName;

                    AjaxControlToolkit.AccordionPane ap = new AjaxControlToolkit.AccordionPane();
                    ap.ID = "ap" + i.ToString();
                    ap.HeaderContainer.Controls.Add(mainMenu);
                    //ap.HeaderContainer.ID = "header" + i.ToString();
                    accMenu.Panes.Add(ap);

                    IList<SysFun> childrenMenus = null;// manage.getChildrenMenusByMainMenuId(main.NodeId);
                    if (Cache["childrenMenus" + loginId + i] != null)
                    {
                        childrenMenus = (List<SysFun>)Cache["childrenMenus" + loginId + i];
                    }
                    else
                    {
                        childrenMenus = manage.getChildrenMenusByMainMenuId(main.NodeId);

                        //添加缓存
                        Cache.Insert("childrenMenus" + loginId + i, childrenMenus, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);//把数据放入缓存
                    }

                    foreach (SysFun child in childrenMenus)
                    {
                        HyperLink hl = new HyperLink();
                        hl.Text = child.DisplayName + "<br/>";
                        hl.NavigateUrl = child.NodeURL;

                        //设置a属性的target
                        string script = string.Format("<script>link('ifrRight');</script>");
                        ScriptManager.RegisterStartupScript(accMenu,this.GetType(),"",script,false);

                        ap.ContentContainer.Controls.Add(hl);
                        //ap.ContentContainer.ID = "content" + i.ToString();
                    }
                
                    
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //MyOA.DAL.LeftPanelService l = new MyOA.DAL.LeftPanelService();
            //l.getSysFunByLoginId("a");
            //MyOA.BLL.LeftPanelManage lm = new MyOA.BLL.LeftPanelManage("admin");
            //lm.getMainMenu();
//dataBind();//绑定数据 功能菜单


            if (!IsPostBack)
            {
                if (Session["loginId"] != null)
                {
                    loginId = (string)Session["loginId"];

                    manage = new LeftPanelManage(loginId);//实例业务层对象

                    dataBind();//绑定数据 功能菜单

                    accMenu.SelectedIndex = 1;
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
            }
        }

        //protected void Page_Unload(object sender, EventArgs e)
        //{
        //    Session.Remove("loginId");
        //}
    }
}
